C PROGRAMMING EXERCISE 3

Exercise 3: Decision making using If and If-else

Youtube Video:

 
CODE IS AS FOLLOWS:

    

// LETS START WITH EXERCISE 3 WHICH IS BASED ON IF AND ELSE STATEMENTS.
//LINK FOR THE CODE OF EXERCISE 1,2 AND 3 WILL BE GIVEN IN THE DESCRIPTION

 #include 
 #include 
 
 void main()
 {
   int opt,n,tax,x,y,pl,cp,sp;
   char c;
   clrscr();
   while(1)
   {
     printf("\nEnter your choice");
     printf("\n1.\t2.\t3.\n4.\t5.\t6.\n7.\t8.\t9.\t(Enter 9 for Exit\n)");
     scanf("%d",&opt);
     
     switch (opt)
     {
       case 1: printf("Q.1:-WRITE A PROGRAM TO ACCEPT INTEGER FROM THE USER AND CHECK WHETHER THE INTEGER IS EVEN OR ODD");
               printf("\nSolution\n"); 
               printf("Enter the integer");
               scanf("%d",&n);
               if(n%2==0)
               {
                 printf("even");
               }
               else{
                 printf("odd");
               }
               
               break;
               
       case 2: printf("Q.2:-ACCEPT A CHARACTER FROM THE USER AND CHECK WHETHER THE CHARACTER IS A DIGIT");
               printf("\nSolution\n");
               printf("Enter the character");
               scanf("%c",&c);
               if(c>=0 && c<=9)
               {
                 printf("Digit is entered");
               }
               else{
                 printf("Non digit");
               }
               
               break;
               
       case 3: printf("Q.3:-WRITE A C PROGRAM WHICH ACCEPTS ANNUAL BASIC SALARY OF AN EMPLOYEE AND CALCULATES AND DISPLAYS THE INCOME TAX");
               printf("\nSolution\n");
               /*TAX RULES:
                 <150000          TAX=0
                 150000 TO 300000 TAX=20%
                 >300000          TAX=30%*/
                printf("Enter your annual income/salary");
                scanf("%d",&n);
                //will use nested if i.e else if
                if(n<150000)
                {
                  printf("there is 0 tax");
                }
                else if(n>=150000 && n<=300000)
                {
                  tax=(n*20)/100;
                  printf("tax=%d",tax);
                }
                else
                {
                  tax=(n*30)/100;
                  printf("tax=%d",tax);
                }
               
               break;
               
       case 4: printf("Q.4:-ACCEPT CHARACTER FROM THE USER AND CHECK WHETHER THE CHARACTER IS A VOWEL OR CONSTANT");
               printf("\nSolution\n");
               printf("Enter the character");
               scanf("%c",&c);
               if (c=='a' || c=='e' || c=='i' ||c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
               {
                 printf("character is a vowel");
               }
               else{
                 printf("character is not a vowel");
               }
               
               break;
               
       case 5: printf("Q.5:-ACCEPT YEARS IN INPUT THROUGH A KEYBOARD.WRITE A PROGRAM TO CHECK WHETHER THE YEAR IS A LEAP YEAR OR NOT");
               printf("\nSolution\n");
               printf("Enter the year");
               scanf("%d",&n);
               // note that leap year is divisible by 4 and not divisible by 100 or 400
               if(n%4==0 && n%100!=0)
               {
                 printf("The year is a leap year");
               }
               else
               {
                 printf("The year is not a leap year");
               }
               
               break;
               
       case 6: printf("Q.6:-WRITE A PROGRAM TO CHECK WHETHER GIVEN CHARACTER IS A DIGIT OR A CHARACTER IN LOWER CASE OR UPPER CASE ALPHABET");
               printf("\nSolution\n");
               printf("Enter the character");
               scanf("%d",&c);
               // note: ASCII value of digit is between 48 to 58 and lower case character is 97 to112 and uppercase is between 65 to 90
               if(c>=48 || c<=58)
               {
                 printf("Value entered is a digit");
               }
               else if(c>=97 || c<=112)
               {
                 printf("Value entered is in Lower case");
               }
               else
               {
                 printf("Value entered is in uppercase");  
               }
               
               break;
               
       case 7: printf("Q.7:-ACCEPT THE X AND Y CORDINATE OF A POINT AND FIND WHICH QUADRANT DOES THE POINT LIES");
               printf("\nSolution\n");
               /* QUADRAND 1=X>0 Y>0
                  QUADRANT 2=X<0 Y>0
                           3=X<0 Y0 Y<0*/
                printf("Enter x and y points");
                scanf("%d%d",&x,&y);
                if(x>0 && y>0)
                {
                  printf("Points lies in quadrant 1");
                }
                else if(x<0 && y>0)
                {
                  printf("Points lies in quadrant 2");
                }
                else if(x<0 && y<0)
                {
                  printf("Points lies in quadrant 3");
                }
                else
                {
                  printf("Points lies in quadrant 4");
                }
                             
               break;
               
       case 8: printf("Q.8 ACCEPT THE COST PRICE AND SELLING PRICE FROM THE KEYBOARD AND FINDOUT WHETHER THE SELLER HAS MADE PROFIT OR NOT");
               printf("\nSolution\n");
               printf("Enter cost price and selinf price");
               scanf("%d%d",&cp,&sp);
               pl=sp-cp;
               if(pl>0)
               {
                 printf("it is profit");
               }
               else
               {
                 printf("loss!");
               }
               
               break;
               
       case 9: exit(1);
       
     }
   }
   //time to check errors..lets swith to another app
   getch();
 }



    
 Download code